#include "pch.h"
#include "Shader.h"
#include "Renderer.h"
#include "GLShader.h"
namespace HS {

	std::shared_ptr<Shader> Shader::Create(const std::string& name,const std::string& vertexSrc, const std::string& fragSrc)
	{
		switch (Render::GetAPI())
		{
		case RendererAPI::API::None:
			LOGINFO("RendererAPI::None is currently not supported!");
			return nullptr;
		case RendererAPI::API::OpenGL:
			return std::make_shared<GLShader>(name,vertexSrc,fragSrc);
		}
		LOGERROR("UnknownAPI!");
		return nullptr;
	}

	std::shared_ptr<Shader> Shader::Create(const std::string& path)
	{
		switch (Render::GetAPI())
		{
		case RendererAPI::API::None:
			//std::cout << "RendererAPI::None is currently not supported!" << std::endl;
			LOGINFO("RendererAPI::None is currently not supported!");
			return nullptr;
		case RendererAPI::API::OpenGL:
			return std::make_shared<GLShader>(path);
		}
		//std::cout << "UnknownAPI!" << std::endl;
		LOGERROR("UnknownAPI!");
		return nullptr;
	}

	void ShaderLibrary::Add(const std::string& name, const std::shared_ptr<Shader>& shader)
	{
		//assert(!Exists(name), "Shader already exists!");
		if (Exists(name))
		{
			LOGERROR("Shader already exists!");
		}
		m_Shaders[name] = shader;
	}

	void ShaderLibrary::Add(const std::shared_ptr<Shader>& shader)
	{
		auto& name = shader->GetName();
		Add(name, shader);
	}

	std::shared_ptr<Shader> ShaderLibrary::Load(const std::string& filepath)
	{
		auto shader = Shader::Create(filepath);
		Add(shader);
		return shader;
	}

	std::shared_ptr<Shader> ShaderLibrary::Load(const std::string& name, const std::string& filepath)
	{
		auto shader = Shader::Create(filepath);
		Add(name, shader);
		return shader;
	}

	std::shared_ptr<Shader> ShaderLibrary::Get(const std::string& name)
	{
		//assert(Exists(name), "Shader not found!");
		if (!Exists(name))
		{
			LOGERROR("Shader not found!");
		}
		return m_Shaders[name];
	}

	bool ShaderLibrary::Exists(const std::string& name) const
	{
		return m_Shaders.find(name) != m_Shaders.end();
	}

}